var app = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationData: "",
    showModalStatus: false,
    isopacity: false,
    showId: null,
    showSendMsg: false,
    blogList: {},
    imgs: null,
    id: null,
    NickName:"",
    Avater:"",
    UpList:[],
    CommentList:[],
    StaffId:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('商圈分享',options);
    var that = this;
    that.setData({ id: options.id })
    that.getBlogList(options.id)
  },

  toComment: function (e) {
    let that = this;
    that.setData({ id: e.currentTarget.dataset.id })
    wx.hideTabBar();
    this.hideModal();
    this.setData({
      showSendMsg: true
    })
  },
  closeMsg: function () {
    wx.showTabBar();
    this.hideModal();
    this.setData({
      showSendMsg: false
    })
  },

  toindexcard:function(){
    let StaffId = this.data.StaffId;
    wx.setStorageSync("cardId", StaffId);
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  
  //获取微博列表
  getBlogList: function (id) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/blog/BlogInfo?blogid=' + id,
      success: function (res) {
        console.log(res)
        var blogList = res.data.Data.BlogInfo;
        if (blogList.Imgs) {
          blogList.Imgs = blogList.Imgs.split(",");
          var imgList = blogList.Imgs;
          for (let i = 0; i < imgList.length; i++){
            imgList[i] = that.trim(that.formatBlogImage(imgList[i]));
          }
          blogList.Imgs = imgList;
        }
        let Avater = utils.formatAvater(res.data.Data.Avater,'https://static.711688.net.cn/upload/card/source/');
        that.setData({ blogList: blogList, NickName: res.data.Data.NickName, Avater: Avater, UpList: res.data.Data.UpList, CommentList: res.data.Data.CommentList, StaffId: res.data.Data.StaffId});
      },
    })
  },


  //路径判断
  formatBlogImage: function (logo) {
    if (logo == null || this.trim(logo) == "") return "";
    if (logo.indexOf("http") > -1) return logo;
    return 'https://static.711688.net.cn/upload/store/' + app.globalData.storeId + '/blog/thumb500_800/' + logo;
  },

  //删除左右两端的空格
  trim: function (str) {
    if (str == null) return "";
    return str.replace(/(^\s*)|(\s*$)/g, "");
  },

  preView: function (e) {
    var that = this
    var index = e.currentTarget.dataset.idx
    var imgs = e.currentTarget.dataset.all
    wx.previewImage({
      current: imgs[index], // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },

  toComment: function (e) {
    let that = this;
    that.setData({ id: e.currentTarget.dataset.id })
    wx.hideTabBar();
    this.hideModal();
    this.setData({
      showSendMsg: true
    })
  },
  closeMsg: function () {
    wx.showTabBar();
    this.hideModal();
    this.setData({
      showSendMsg: false
    })
  },

  //获取评论框内容
  getContent: function (e) {
    this.setData({ content: e.detail.value })
  },
  //添加评论
  addBlogComment: function () {
    var that = this;
    if (that.data.content == "") {
      wx.showToast({
        title: '内容不能为空！',
        icon: 'success',
        duration: 2000
      })
      return
    }
    wx.request({
      url: app.globalData.host + '/api/Blog/AddBlogComment?blogid=' + that.data.id + '&uid=' + app.globalData.userInfo.Uid + '&Content=' + that.data.content,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({ content: "" })
          that.closeMsg();
          that.getBlogList(that.data.id);
        }
      },
    })
  },
  //点赞功能
  addUp: function (e) {
    let that = this;
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.host + '/api/Blog/AddUp?blogId=' + id + '&uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          that.hideModal();
          that.getBlogList(that.data.id);
        }
      },
    })
  },

  showModal: function (e) {
    var id = e.currentTarget.dataset.id;
    // 显示遮罩层  
    if (this.data.showModalStatus && id == this.data.showId) {
      this.hideModal();
      return
    }
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.width("0rpx").step()
    animation.width("300rpx").step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true,
      showId: id
    })
    setTimeout(function () {
      animation.translateX(0).step()
      this.setData({
        animationData: animation.export(),
      })
    }.bind(this), 200)
    var that = this;
    setTimeout(function () {
      that.setData({
        isopacity: true
      })
    }.bind(this), 400)
  },
  hideModal: function () {
    // 隐藏遮罩层  
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation;
    animation.width("0rpx").step()
    this.setData({
      animationData: animation.export(),
      isopacity: false
    })
    var that = this;
    setTimeout(function () {
      animation.translateX(0).step()
      that.setData({
        animationData: animation.export(),
        showModalStatus: false,
        showId: that.data.showId
      })
    }.bind(this), 200)
  },
  
})